/**
 * Created by liuqingxia on 2016/4/11.
 */

var count = 0;// 进度条计数器
var intervalId;// 循环方法Id
var progressId = "#progressBar";
var progressBarDivId = "#progressBarDiv";

var token = true;// 防止重复提交

$(document).ready(
		function() {
			var allDataSourceDiv = getAllDataSourceDiv("allDataSource-div",
					"/datasource/findAll", "master");
			allDataSourceDiv.init();
			var allBackUpDataSourceDiv = getAllDataSourceDiv(
					"allBackUpDataSource-div", "/datasource/findAll", "slave");
			allBackUpDataSourceDiv.init();
			clickSourceInsertBtn("selectedDataSource-div", "master");
			clickBackSourceInsertBtn("backUpSelectedDataSource-div", "slave");
			clickSourceRemoveBtn();
			clickGroupInsertCommitBtn();
			getGroupName();
			hasBak();
		});

/**
 * 进度条滚动...
 */
function progressing() {
	count = count + 1;
	if (count > 100) {
		window.clearInterval(intervalId);
		count = 0;
		return;
	}
	$(progressId).css("width", count + "%");
}

/**
 * 进度条滚动
 */
function setIntervalProgressBar() {
	intervalId = self.setInterval("progressing()", 100);
}

/**
 * 进度条显示
 */
function showProgressBar() {
	$(progressBarDivId).show();
}

/**
 * 隐藏进度条
 */
function hideProgressBar() {
	$(progressBarDivId).hide();
}

/**
 * 清空进度条
 */
function clearProgressBar() {
	window.clearInterval(intervalId);
	$(progressId).css("width", "0%");
}

/**
 * 完成进度条
 */
function fullProgressBar() {
	window.clearInterval(intervalId);
	count = 0;
	$(progressId).css("width", "100%");
}

/**
 * 点击AppInsert的提交按钮
 */
function clickGroupInsertCommitBtn() {
	$(document).on("click", "#groupInsertCommit-btn", function() {
		var flag = true;
		var url = getUrlBySuffix("/tddl/grop/saveGroup");
		var params = getParams();
		console.log(params);
		for ( var p in params) {
			console.log(p);
			console.log(params[p]);

			if (p == "databaseList") {
				if (params[p] == "false") {
					swal("警告", "数据源参数有问题请检查", 'warning');
					flag = false;
					return;
				}
			}
			if(p == "backupDataList"){
				var hasBackUp = $("input[name=hasBackUp]:checked").val();
				if("1"==hasBackUp){
					if(params[p] == "false"){
						swal("警告", "备库数据源参数有问题请检查", 'warning');
						flag = false;
						return;	
					}
				}
			}
			
			
			if (isNull(params[p]) || params[p] == false) {

				swal("警告", "表单内容有误请检查", 'warning');
				flag = false;
				return;
			}
		}

		 showProgressBar();
		 setIntervalProgressBar();

		// console.log(JSON.stringify(params));
		if (token && flag) {
			$.postHttp(params, url, function(json) {
				if (json && json.success) {
					fullProgressBar();
					swal("成功", json['message'], "success");
					token = false;
				} else {
					swal("警告", json['message'], "error");
				}
				hideProgressBar();
				clearProgressBar();
			});
		}

	});
}
function getParams() {
	var param = {};
	param.appName = $.trim($("input[name=appName]").val());
	param.groupName = $.trim($("input[name=groupName]").val());
	param.version = $.trim($("#version option:selected").val());
	param.projectName = $.trim($(".projectName option:selected").val());
	param.databaseList = getDBlist().toString();
	param.backupDataList=getBackupDBlist().toString();
	return param;
}
function getDBlist() {
	var dblist = "[";
	var dataSources = $("#selectedDataSource-div").find(".dataSource-row");
	if (dataSources.length == 0) {
		swal("警告", "请选择一个数据源", "error");
		return false;
	}
	var count = 0;
	for (var i = 0; i < dataSources.length; i++) {

		var dataSource = dataSources[i];
		var dbName = $.trim($(dataSource).find("input[name=dbname]").val())
				+ "_" + i;
		var readWeight = Number($.trim($(dataSource).find(
				"input[name=readWeight]").val()));
		var writeWeight = Number($.trim($(dataSource).find(
				"input[name=writeWeight]").val()));
		var datasourceId = $.trim($(dataSource).find("input[name=dbid]").val());
		var jdbcUrl = $.trim($(dataSource).find("input[name=dbJdbcUrl]").val());
		var datasourcePort = $.trim($(dataSource).find("input[name=choiceDbPort]").val());
		var datasourceIp = $.trim($(dataSource).find("input[name=choiceDbIp]").val());
		if (readWeight == 0 && writeWeight == 0) {
			swal("警告", "请选择读权重和写权重不可都为0", "error");
			count = 0;
			break;
		}
		var rowValue = '{"dbName":"' + dbName + '","readWeight":"' + readWeight
		+ '","writeWeight":"' + writeWeight + '","datasourceId":"'
		+ datasourceId + '","jdbcUrl":"' + jdbcUrl + '","datasourcePort":"'+datasourcePort+'","datasourceIp":"'+datasourceIp+'"},';
		dblist += rowValue;
		count++;
	}
	if (count == 0) {
		return false;
	} 
	dblist = dblist.substring(0, dblist.lastIndexOf(","));
	dblist += "]";
	return dblist;
}

function getBackupDBlist(){
	var hasBackUp=$("input[name=hasBackUp]:checked").val();
	if("0"==hasBackUp){
		return false;
	}
	var dblist = "[";
	var dataSources = $("#backUpSelectedDataSource-div").find(".dataSource-row");
	var count = 0;
	for (var i = 0; i < dataSources.length; i++) {

		var dataSource = dataSources[i];
		var dbName = $.trim($(dataSource).find("input[name=dbname]").val())
				+ "_" + i+"_bak";
		var readWeight = 0;
		var writeWeight = 0;
		var datasourceIp = $.trim($(dataSource).find("input[name=choiceDbIp]").val());
		var datasourcePort = $.trim($(dataSource).find("input[name=choiceDbPort]").val());
		var datasourceId = $.trim($(dataSource).find("input[name=dbid]").val());
		var jdbcUrl = $.trim($(dataSource).find("input[name=dbJdbcUrl]").val());
		
		var rowValue = '{"dbName":"' + dbName + '","datasourceId":"'
				+ datasourceId + '","jdbcUrl":"' + jdbcUrl + '","datasourcePort":"'+datasourcePort+'","datasourceIp":"'+datasourceIp+'"},';
		dblist += rowValue;
		count++;
	}
	if (count == 0) {
		return false;
	}
	dblist = dblist.substring(0, dblist.lastIndexOf(","));
	dblist += "]";
	return dblist;
}

function getGroupName() {
	$(document).on("keyup", "input[name=appName]", function() {
		var suffix = "_group_0";
		var thisValue = $(this).val();
		var groupValue = thisValue + suffix;
		$("input[name=groupName]").val(groupValue);
	})
}

function hasBak(){
	$(document).on("click","input[name=hasBackUp]",function(){
		var thisvalue=$(this).val();
		if("0"==thisvalue){
			$("#backUpSourceDiv").hide();
		}else{
			$("#backUpSourceDiv").show();
		}
	})
}
